package com.example.javacrawl.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.javacrawl.mapper.LeftGraphMapper;
import com.example.javacrawl.pojo.LeftGraphData;
import com.example.javacrawl.pojo.RightGraphData;
import com.example.javacrawl.service.LeftGraphDataService;
import com.example.javacrawl.util.AutoGetDataUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Service
public class LeftGraphDataServiceImpl extends ServiceImpl<LeftGraphMapper, LeftGraphData> implements LeftGraphDataService {


    @Resource
    private LeftGraphMapper leftGraphMapper;
    public void handleData(){

        HashMap<String ,Object> dataMap = AutoGetDataUtil.getData();
        if(dataMap == null)return;
        if(dataMap.containsKey("leftGraph")){
            List<LeftGraphData> leftGraphDataList = (List<LeftGraphData>) dataMap.get("leftGraph");
            for(LeftGraphData leftGraphData : leftGraphDataList){
                System.out.println(leftGraphData.getYearMonth());
                if(leftGraphData.getYearMonth() == null || leftGraphData.getYearMonth() == "")continue;
                QueryWrapper<LeftGraphData> queryWrapper = new QueryWrapper<>();


                Integer count = query().eq("d_year_month", leftGraphData.getYearMonth()).count();

                if(count == 0){
                    // 插入数据
                    System.out.println("保存数据");
                    System.out.println(leftGraphData);
                   save(leftGraphData);
                }else{


                    // 判断数据是否有变化
                    Integer cnt = query()
                            .eq("d_year_month", leftGraphData.getYearMonth())
                            .eq("trade_amt", leftGraphData.getTradeAmt())
                            .eq("amt", leftGraphData.getAmt())
                            .eq("order_cnt", leftGraphData.getOrderCnt()).count();
                    if(cnt == 1){
                        System.out.println("不用更新");
                    }else{
                        System.out.println("需要更新哟");
                        UpdateWrapper<LeftGraphData> updateWapper = new UpdateWrapper<>();
                        updateWapper.eq("d_year_month", leftGraphData.getYearMonth());
                        leftGraphMapper.update(leftGraphData,updateWapper);

                    }
                };
            }
        }
    }

}
